//同步角色数据
Game_Actor.prototype.setupActorData = function (data) {
    const { battlerName, classId, level, exp, equips, skills, abilityPoints } = data;
    this._battlerName = battlerName;
    this._classId = classId + 1;
    // this._level = level;
    this.changeExp(exp, false);
    // this._equips = equips ?? this._equips;
    this._skills = skills ?? this._skills;
    this._pointsBase = abilityPoints ?? this._pointsBase;
    this.updateEquip(equips);
    this.refreshPoints();
    // this._battlerName = battlerName;
}

//同步角色装备
Game_Actor.prototype.updateEquip = function (data) {
    if (!data) return;
    const equips = this.equips();
    for (let i = 0; i < equips.length; i++) {
        const id = data[i];
        const item = id === 0 ? null : i === 0 ? $dataWeapons[id] : $dataArmors[id];
        this._equips[i].setObject(item);
    }
}

//获得经验
const Net_changeExp = Game_Actor.prototype.changeExp;
Game_Actor.prototype.changeExp = function (exp, show) {
    Net_changeExp.call(this, ...arguments);
    const data = {};
    data.exp = this.currentExp();
    data.level = this.level;
    NetManager.dataServer.emit('change exp', data);
}

/* 更换装备 */
const Net_changeEquip = Game_Actor.prototype.changeEquip;
Game_Actor.prototype.changeEquip = function (slotId, item) {
    Net_changeEquip.call(this, ...arguments);
    const data = {};
    data.slotId = slotId;
    data.itemId = item ? item.id : 0;
    NetManager.dataServer.emit('change equip', data);
}

//获取装备id
Game_Actor.prototype.getEquipsId = function () {
    return this._equips.map((item) => {
        if (item.isNull()) {
            return 0;
        } else {
            return item.itemId();
        }
    })
}

class NetData {

    //创建socket事件，将在选择角色进入游戏后执行
    static createSocketEvent() {
        const socket = NetManager.dataServer;
        //
        socket.on("actor data", (data) => {
            NetManager.netActor().setupActorData(data);
        })

        //更新点数结果
        socket.on("update points result", (points) => {
            const baseWindow = SceneManager._scene._baseWindow;
            const tipWindow = SceneManager._scene._tipWindow;
            const actor = NetManager.netActor();
            actor._pointsBase = points;
            actor.refreshPoints();
            if (baseWindow) {
                baseWindow.iniPoints();
                baseWindow.refresh();
                tipWindow.pop("加点成功");
            }
        })
    }
}